// pages/home/home.js
/*{
单元名称：方通首页菜单
单元功能：点击菜单切换到不同子系统
所属模块功能：
创建人：yan
创建时间：231122
修改人：
修改时间：
}*/
import {wxLogin} from "../../utils/login";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [],
    showShade: true
  },

  navTo(e){
    let {type, url}= e.currentTarget.dataset;

    if (type === 'admin'){
      wx.switchTab({
        url: '/pages/index/index',
      });
      return
    }
    if (type === 'driver'){
      let driver_row_id = wx.getStorageSync('driver_row_id');
      if (!driver_row_id){
        /** 231216 liang 注释
        wx.showModal({
          title: '未绑定司机',
          content: '请联系管理员绑定司机资料',
          showCancel: false
        })
        */
        // 231216 liang 按方通张小姐要求，改为 确定 跳转 司机注册 页
        wx.showModal({
          title: '未绑定司机',
          content: '请联系管理员绑定司机资料',
          confirmText: '去注册',
          cancelText: '取消',
          success(res) {
            if(res.confirm) {
              wx.navigateTo({
                url: '/SlagCar/Register/Register',
              })
            }
          },
        })
        // 231216 end
        return
      }
      url = '/SlagCar/Index/Index'
      wx.setStorageSync('user_type','driver')
    }
    if (type === 'system'){
      url = '/SlagCar/Index/Index';
      wx.setStorageSync('user_type','system')
    }
    if (type === 'excavator'){
      url = '/SlagCar/OA/OA'
    }
    if (type === 'outsideCar'){
      url = '/SlagCar/Index/Index';
      wx.setStorageSync('user_type','carTeam')

    }
    if (type === 'disposalArea'){
      url = '/SlagCar/Index/Index';
      wx.setStorageSync('user_type','disposal')

    }
    if (type === 'logout'){
      wx.showModal({
        content: '确认退出登录？',
        success: (res) => {
          if(res.confirm) {
            // 清空登录信息相关cookie，跳回登录页
            let rlatKey = ['refresh', 'token', 'company_name', 'company_id', 'user_name', 'user_id', 'role_id', 'is_platfrom', 'is_agent', 'userLevel',
            'driver_row_id','driver_id','driver_name','driver_car_row_id','driver_car_id','driver_car_name']
            rlatKey.forEach(key => wx.removeStorageSync(key))
            wx.reLaunch({
              url: '../login/login',
            })
          }
        },
      })
    }
    if (type === 'developing') {
      wx.$toast('功能正在开发中')
      return
    }

    wx.navigateTo({url})
  },


  // 获取员工信息
  getEmployeeData(){
    return new Promise(resolve => {
      let employee_row_id = wx.getStorageSync('employee_row_id');
      console.log(employee_row_id);
      if (!employee_row_id) {
        resolve();
        return
      }
      let post = {
        "oby":"all_employee_inf.row_id desc",
        "table_id":"all_employee_inf",
        "page":1,
        "limit":1,
        "SearchWhere":{
          "type": "and",
          "filters" : [
            {
              "CompareValue": employee_row_id,
              "CompareWith": "all_employee_inf.row_id",
              "Operator": "=",
              "or": 0
            }
          ]
        }

      }
      // wx.$post(3,'DynamicQuery/query_dynamic_v3',post).then(res =>{
      wx.$get(5,'ZTAuth/EmploveeBelonging',{row_id: employee_row_id}).then(res =>{
        if (res.errcode != 0 ) return;
        let data = res.data && res.data.row[0];
        if (!data){
          resolve();
          return
        }
        let suoshuchedui = data && data.suoshuchedui,
          suoshuxiaonachang = data && data.suoshuxiaonachang,
          //对账入口，员工资料有设置所属单位及对账人员标识才可进入
          suoshudanwei = data && data.suoshudanwei && data.duizhangrenyuanbiaoshi
        /*this.setData({
          suoshuchedui,
          suoshuxiaonachang
        });*/
        resolve(data);
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    wx.$setBarTitle();
    //231218 yan 增加初始进入判断登录信息，没有登录信息则登录，登录不成功跳转到登录页，增加遮罩层
    wx.showLoading();
    if (wx.getStorageSync('token')){
      this.setData({showShade: false})
      wx.hideLoading();
    } else {
      wxLogin(1164).then(data =>{
        wx.$util.setLoginStorage(data);
        this.setData({showShade: false})
      },() => {
        wx.reLaunch({
          url: '/pages/login/login',
        })
      });
    }
  //  231218 end

  //  231225 yan 增加判断司机角色，如果司机角色为【内部司机、挂靠司机、外请司机】则不可查看管理员页面
    let navList = [/*
      {title: '企业管理', icon: '',style:'#01c847',id: 'admin',},
      {title: '司机运输', icon:'',style: '#3588f3',id: 'driver',},
      {title: '运输管理', icon:'',style: '#ff962e',id: 'system',},
      {title: '挖机铲车', icon:'',style: '#ffce31',id: 'developing',},
      {title: '渣土清运', icon:'',style: '#53b4ee',id: 'developing',},
      {title: '沙石销售', icon:'',style: '#53b4ee',id: 'developing',},
      {title: '收纳渣土', icon:'',style: '#53b4ee',id: 'developing',},
      {title: '退出登录', icon:'',style: '#ff4943',id: 'logout',}
    */],
      role_name = wx.getStorageSync('role_name'),
      role_id = wx.getStorageSync('role_id');
    //企业管理
    if (['财务部','系统管理员','系统研发人员'].includes(role_name)){
      navList.push({title: '企业管理', icon: '',style:'#01c847',id: 'admin',})
    }
    //司机运输
    if (['内部司机','外请司机','挂靠司机','系统研发人员','财务部','系统管理员'].includes(role_name) || role_name.indexOf('合伙') >=0){
      navList.push({title: '司机运输', icon:'',style: '#3588f3',id: 'driver',})
    }
    //运输管理
    if (['财务部','系统管理员','系统研发人员','施工员','行政部'].includes(role_name)){
      navList.push({title: '运输管理', icon:'',style: '#ff962e',id: 'system',})
    }
    //挖机铲车
    if (['工程部','行政部','财务人员','系统管理员','施工员','系统研发人员'].includes(role_name)){
      navList.push({title: '挖机铲车', icon:'',style: '#ffce31',id: 'excavator',})
    }
    await this.getEmployeeData().then(data =>{
      if (data && data.suoshuchedui) {
        navList.push({title: '外部车队管理', icon:'',style: '#ff8749',id: 'outsideCar',})
      }
      if (data && data.suoshuxiaonachang) {
        navList.push({title: '消纳场管理', icon:'',style: '#c16100',id: 'disposalArea',})
      }
      if (data && data.suoshudanwei){
        navList.push({title: '对账查询', icon:'',style: '#c1b485',id: '',url:'/SlagCar/AccountChecking/AccountChecking',})
      }
      console.log(data);
      if (data && data.shenhequxiaoyundan){
        navList.push({title: '取消运单审核', icon:'',style: '#71cb86',id: '',url:'/SlagCar/WaybillCancelApplyList/WaybillCancelApplyList',})
      }
    })

    navList.push(
      // {title: '外部车队管理', icon:'',style: '#ff8749',id: 'outsideCar',},
      // {title: '消纳场管理', icon:'',style: '#c16100',id: 'disposalArea',},
      {title: '渣土清运', icon:'',style: '#53b4ee',id: 'developing',},
      {title: '沙石销售', icon:'',style: '#53b4ee',id: 'developing',},
      {title: '收纳渣土', icon:'',style: '#53b4ee',id: 'developing',},
      // {title: '退出登录', icon:'',style: '#ff4943',id: 'logout',})
      {title: '个人中心', icon:'',style: '#ff4943',id: 'personal',url:'/SlagCar/Personal/Personal'})
    /*if (['009','010','011'].includes(role_id)) {
      navList = [
        {title: '司机运输', icon:'',style: '#3588f3',id: 'driver',},
        {title: '挖机铲车', icon:'',style: '#ffce31',id: 'developing',},
        {title: '渣土清运', icon:'',style: '#53b4ee',id: 'developing',},
        {title: '沙石销售', icon:'',style: '#53b4ee',id: 'developing',},
        {title: '收纳渣土', icon:'',style: '#53b4ee',id: 'developing',},
        {title: '退出登录', icon:'',style: '#ff4943',id: 'logout',}
        ]
    }*/
    // navList.push({title: '测试电子围栏', icon: '',style:'#ffce31',url: '/pages/testMapPolhgon/testMapPolhgon'},)
    this.setData({navList})

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
